package com.gjszn.kh.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

/**    
* @Title: ImageUtil.java  
* @Package com.gjszn.kh.utils    
* @author jys@nx-yt.com    
* @date 2016年1月12日 上午9:50:25  
* @version V1.0    
* @Description: TODO(用一句话描述该文件做什么)
********************************************/
public class ImageUtil {
	private static final int YZM_W = 80;
	private static final int YZM_H = 25;
	
	
	public static void drawNumber(int w, int h, String number, HttpServletResponse response){
		BufferedImage imageCode = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics graphics = imageCode.getGraphics(); // 获取图片画笔
		//1. 设置背景颜色
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, w, h);
		//2. 绘制边框线
		//graphics.setColor(Color.BLUE);
		//graphics.drawRect(1, 1, w-2, h-2);
		
		Graphics2D graphics2D = (Graphics2D)graphics; // 向下转型
		graphics2D.setFont(new Font("Consolas", Font.BOLD, 24));
		graphics.setColor(Color.RED);
		graphics.drawString(number, 5, 23);
		// 6. 输出到浏览器
		try {
			ImageIO.write(imageCode, "jpg", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void createYZM(HttpServletRequest request, HttpServletResponse response){
		BufferedImage imageCode = new BufferedImage(YZM_W, YZM_H, BufferedImage.TYPE_INT_RGB);
		Graphics graphics = imageCode.getGraphics(); // 获取图片画笔
		Random random = new Random(); // 随机数生成对象
		//1. 设置背景颜色
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, YZM_W, YZM_H);
		
		//2. 绘制边框线
		graphics.setColor(Color.BLUE);
		graphics.drawRect(1, 1, YZM_W-2, YZM_H-2);
		
		//3. 绘制干扰线
		graphics.setColor(Color.GREEN);
		for(int i=0; i<4; i++){
			int x1 = random.nextInt(YZM_W);
			int y1 = random.nextInt(YZM_H);
			
			int x2 = random.nextInt(YZM_W);
			int y2 = random.nextInt(YZM_H);
			
			graphics.drawLine(x1, y1, x2, y2); // 绘制线条
		}
		
		//4. 绘制验证码信息
		Graphics2D graphics2D = (Graphics2D)graphics; // 向下转型
		graphics2D.setColor(Color.BLUE);
		graphics2D.setFont(new Font("Consolas", Font.BOLD, 18));
		StringBuffer sb = new StringBuffer(); // 验证信息保存对象
		int startX = 5; // 旋转的初始位置
		for(int i=0; i<4; i++){
			int number = random.nextInt(10); 
			int radian = random.nextInt() % 30; // 产生-30到30直接的随机数
			
			graphics2D.rotate(radian*Math.PI/180, startX, 20); // 旋转角度
			
			graphics2D.drawString(number+"", startX, 20);
			
			graphics2D.rotate(-radian*Math.PI/180, startX, 20);
			
			sb.append(number); //存放验证信息
			startX += 20;
		}
		// 5. 存放到session中
		request.getSession().setAttribute("yzm", sb.toString());
		// 6. 输出到浏览器
		try {
			ImageIO.write(imageCode, "jpg", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	public static void createQrcode(String content, int width, int height, String format, HttpServletResponse response){
		try {
			Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();  
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content,  
			        BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵  
			MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());
		} catch (WriterException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
	}
	
}

